package com.dl.config;

import com.dl.properties.ConfigProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author DL
 * @create 2021-01-30 10:11
 */
@Configuration
@EnableSwagger2//开启swagger2
@EnableConfigurationProperties(ConfigProperties.class)
public class SwaggerConfig {

    @Resource
    private ConfigProperties configProperties;

    @Bean
    public Docket docket(Environment environment){

        boolean swaggerEnable = configProperties.isSwaggerEnable();
/*        Profiles profiles = Profiles.of("prod");
        //获取项目环境：通过 environment.acceptsProfiles 判断是否处于生产环境
        boolean flag = !environment.acceptsProfiles(profiles);*/
        System.out.println("是否开启swagger = " + swaggerEnable);

        //配置swaggerDocket实例
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(swaggerEnable)//是否启用swagger
                .groupName("DL")
                //RequestHandlerSelectors：配置要扫描的包
                //basePackage("com.dl.controller")：指定要扫描的包
                //any()：扫描全部
                //none()：不扫描
                //withClassAnnotation()：扫描类上的注解，参数是一个注解的反射对象
                //withMethodAnnotation()：扫描方法上的注解
                //.select().apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)).build();
                //.select().apis(RequestHandlerSelectors.basePackage("com.dl.controller"))
                .select().apis(RequestHandlerSelectors.withClassAnnotation(Api.class))          //只扫描有api注解的类
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))//只扫描有ApiOperation注解的方法

                //过滤什么路径
                //.paths(PathSelectors.ant("/dl/**"))
                .build();
    }

/*    @Bean
    public Docket docket2(Environment environment){
        //配置swaggerDocket实例
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");

    }*/

    //配置swagger信息
    private ApiInfo apiInfo(){

        //作者信息
        Contact DEFAULT_CONTACT = new Contact("丁亮", "http://localhost:8080/", "2458697735@qq.com");
        return new ApiInfo(
                "DL的SwaggerAPI文档",
                "这个作者有点酷",
                "v1.0",
                "http://localhost:8080/",
                DEFAULT_CONTACT,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

}
